package com.erps.CommonUtils;

import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

/**
 * ��ȡ��ҳ��
 * 
 * @author __LuckyStar
 * @date 20110416
 */
public class CaptureBrower {

	/**
	 * ��ȡָ����ҳ
	 * 
	 * @param url
	 *            ��ַ
	 * @param folderName
	 *            ͼƬ����Ŀ¼
	 * @throws IOException
	 * @throws URISyntaxException
	 * @throws AWTException
	 */
	private void capture(String url, String folderName) throws IOException,
			URISyntaxException, AWTException {
		// �������
		Desktop.getDesktop().browse(new URI(url));
		Robot robot = new Robot();
		// ��������
		robot.keyRelease(KeyEvent.VK_F11);
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		// �ӳ�2S��ȡ��Ļ
		robot.delay(6000);
		// ��ȡ��Ļ
		Image image = robot.createScreenCapture(new Rectangle(width, height));
		// ����ͼƬ
		String fileName = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date());
		ImageIO.write((RenderedImage) image, "jpg", new File(folderName
				+ File.separator + fileName + ".jpg"));
		System.out.println(folderName + fileName);
	}

	/**
	 * �ж�ͼƬ����Ŀ¼�Ƿ���ڡ�<br>
	 * �������򴴽���
	 */
	private void checkFolder(String folderName) {
		File dir = new File(folderName);
		if (!dir.exists() || !dir.isDirectory()) {
			dir.mkdirs();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String folderName = "images";
		CaptureBrower cb = new CaptureBrower();
		cb.checkFolder(folderName);
		try {
			cb.capture("http://qcd.iteye.com/blog/1004831", folderName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
